const vm = new Vue({
    el: '#app',
    data: {
        tableData: [],//表格当前页数据
        pagesize: 20,//默认每页数据量
        curRowData: {}, //当前选择行数据
        currentPage: 1,//当前页码
        start: 1, //查询的页码
        totalCount: '',//默认数据总数
        multipleSelection: [],//多选数组
        showList: true,
        dialogView: false,
        randoms: [],//随机数集合
        avg: '',//平均倍数

    },
    created() {
        Vue.nextTick(() => {
            //载入数据
            vm.loadData(vm.currentPage, vm.pagesize);
        });
    },
    methods: {
        //从后台获取数据
        loadData: function (page, limit) {
            const params = {page, limit}
            $.ajax({
                type: "POST",
                url: baseURL + "business/orderscholarshipconfig/list",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code === 0) {
                        vm.tableData = r.page.list
                        vm.totalCount = r.page.totalCount
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //多选响应
        handleSelectionChange: function (val) {
            vm.multipleSelection = val;
        },
        //点击行响应
        handleclick: function (row, event, column) {
            vm.curRowData = JSON.parse(JSON.stringify(row));//深克隆
        },
        //弹出编辑框
        handleEdit: function (index, row) {
            vm.curRowData = JSON.parse(JSON.stringify(row));//深克隆
            vm.dialogFormVisible = true
        },
        //提交编辑
        edit() {
            $.ajax({
                type: "POST",
                url: baseURL + "userTeam/userteam/update",
                contentType: "application/json",
                data: JSON.stringify(vm.curRowData),
                success: function (r) {
                    if (r.code === 0) {
                        vm.loadData(vm.currentPage, vm.pagesize);//刷新后页面在当前页
                        //vm.loadData(vm.currentPage=1, vm.pagesize);刷新后页面回到首页
                        vm.dialogFormVisible = false
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //每页显示数据量变更
        handleSizeChange: function (val) {
            vm.pagesize = val;
            vm.loadData(vm.currentPage, vm.pagesize);
        },
        //页码变更
        handleCurrentChange: function (val) {
            vm.currentPage = val;
            vm.loadData(vm.currentPage, vm.pagesize);
        },
        //修改随机数页面
        updateRandom(index,row) {
            vm.randoms = []
            vm.randoms = row.dataValue.split(',')
            vm.showList = false
        },
        //修改随机数
        updateRandomHandler() {
            let flag=true
            vm.randoms.forEach(v => {
                if (v <= 0 || v >= 20) {
                    flag=false
                    return false
                }
            })
            if (!flag) {
                alert('请检查数字有效性')
                return
            }
            let set = new Set();
            vm.randoms.map(x => set.add(x));
            if (vm.randoms.length != set.size) {
                alert('请勿添加重复数字')
                return
            }

            const dataValue = vm.randoms.join(',');
            $.ajax({
                type: "POST",
                url: baseURL + "business/orderscholarshipconfig/updateRandomHandler",
                contentType: "application/json",
                data: dataValue,
                success: function (r) {
                    if (r.code === 0) {
                        alert('修改成功')
                        vm.loadData(vm.currentPage, vm.pagesize);//刷新后页面在当前页
                        //vm.loadData(vm.currentPage=1, vm.pagesize);刷新后页面回到首页
                        vm.showList = true
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //修改倍数页面
        updateAvg() {
            vm.randoms=[]
            vm.randoms=vm.tableData[0].dataValue.split(',')
            vm.dialogView = true
        },
        updateAvgHandler() {
            if (!vm.avg) {
                alert('请选择平均倍数')
                return
            }
            $.get(baseURL + "business/orderscholarshipconfig/updateAvgHandler/"+vm.avg, function(r){
                alert('修改成功')
                vm.loadData(vm.currentPage, vm.pagesize);//刷新后页面在当前页
                vm.dialogView = false
            });
        },
        addRandom: function () {
            vm.randoms.push('')
        },
        delRandom: function (index) {
            if (vm.randoms.length > 1) {
                vm.randoms.splice(index, 1);
            }
        },

    }
});
